﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lswweb.Domain;

namespace Lswweb.Controls
{
    public class FlashBanner : SkinnedUserControl
    {
        public event Func<FlashBannerCollection> FlashBannerCollectionHandler;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);


            if (string.IsNullOrEmpty(this.FlashUrl))
                this.FlashUrl = "/Flash/bcastr3.swf";

            if (string.IsNullOrEmpty(this.FlashLinkVarName))
                this.FlashLinkVarName = "bcastr_link";

            if (string.IsNullOrEmpty(this.FlashImageVarName))
                this.FlashImageVarName = "bcastr_file";

            if (string.IsNullOrEmpty(this.BannerPanelJQueryExpr))
                this.BannerPanelJQueryExpr = "#banner";

            if(this.BannerCollection!=null)
                Page.ClientScript.RegisterClientScriptBlock(typeof(FlashBanner), this.ClientID, ScriptBlock(), true);
        }

        public string ScriptBlock()
        {
            StringBuilder scriptBuilder = new StringBuilder();
            scriptBuilder.Append("$(function(){");

            var vars = string.Format(
                "{0}:\"{1}\",{2}:\"{3}\"",
                this.FlashImageVarName,
                this.BannerCollection.ImageUrls,
                this.FlashLinkVarName,
                this.BannerCollection.LinkUrls);

            if (!string.IsNullOrEmpty(this.FlashTitleVarName))
            {
                vars = string.Format(
                    "{0},{1}:\"{2}\"",
                    vars,
                    this.FlashTitleVarName,
                    this.BannerCollection.Titles);
            }

            if (!string.IsNullOrEmpty(this.OtherVars))
            {
                vars += "," + this.OtherVars.TrimStart(',');
            }

            scriptBuilder.Append("$(\"" + this.BannerPanelJQueryExpr + "\").flashembed({src:\"" + Page.GetThemeElement(this.FlashUrl) + "\",wmode:\"opaque\"},{" + vars + "});");

            scriptBuilder.Append("});");

            return scriptBuilder.ToString();
        }

        private FlashBannerCollection bannerCollection;
        private FlashBannerCollection BannerCollection
        {
            get
            {
                if (this.bannerCollection == null && this.FlashBannerCollectionHandler != null)
                    this.bannerCollection = this.FlashBannerCollectionHandler();

                return this.bannerCollection;
            }
        }

        public string FlashUrl { get; set; }

        public string FlashLinkVarName { get; set; }

        public string FlashTitleVarName { get; set; }

        public string FlashImageVarName { get; set; }

        /// <summary>
        /// 格式varName:"varValue",var2Name:"var2Value"
        /// </summary>
        public string OtherVars { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string BannerPanelJQueryExpr { get; set; }
    }
}
